@extends('admin.common.base-master')

@section('html-head')
    <link href="{{url('css/admin/pagenum.css')}}" rel="stylesheet" />
@endsection

@section('html-body')
    <!-- content start -->
    <div id="content">
        <!-- cur-position start -->
        <div id="cur-position">
            <span class="tit">当前位置：</span>
            <a href="">医院列表</a>
        </div>
        <!-- cur-position end -->

        <!-- filter-box start -->
        <div class="filter-box clearfix">
            <div class="fr">
                <a class="addBtn" href="{{route('Admin.Index.hospitalAdd')}}">新增</a>
            </div>
        </div>
        <!-- filter-box end -->

        <div class="table-box">
            <table class="table">
                <thead>
                <tr>
                    <th>ID</th>
                    <th>医院名称</th>
                    <th>科室名称</th>
                    <th>联系电话</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                @if($hospitals && count($hospitals)>0)
                    @foreach($hospitals as $hospital)
                        <tr id="hos_{{$hospital->id}}">
                            <td>{{$hospital->id}}</td>
                            <td>{{$hospital->name}}</td>
                            <td>
                                @if(count($hospital->keshis) > 0)
                                    @foreach($hospital->keshis as $keshi)
                                        <p id="dep_{{$keshi->id}}">{{$keshi->dep_name}}<a href="javascript:void(0)" id="del_dep" class="del_dep" style="color: red;margin-left: 3px" data-id="{{$keshi->id}}">删除</a></p>
                                    @endforeach
                                @endif
                            </td>
                            <td>{{$hospital->telephone}}</td>
                            <td class="btna">
                                <a href="javascript:void(0);" class="set_sort" data-id="{{$hospital->id}}" style="color: #12aebf;">置顶</a>| <a href="javascript:void(0)" onclick="upPop({{$hospital->id}})">添加科室</a>| <a href="javascript:void(0);" class="del_hospital" data-id="{{$hospital->id}}" style="color: #ef1414;">删除</a>
                            </td>
                        </tr>
                    @endforeach
                @endif
                </tbody>
                <tfoot class="order-gl">
                </tfoot>
            </table>
        </div>
        <div>
            {!! $hospitals->render() !!}
        </div>
    </div>

    <div class="upPop-box">
        <div class="backdrop"></div>
        <div id="upPop" class="info-pop tixian">
            <a class="close" href="javascript:void(0);"></a>
            <h3>添加科室</h3>
            <div class="fill-list">
                <span class="fill-tit">科室名称：</span>
                <input class="fill-ipt" type="text" name="classify_name" id="classify_name">
            </div>

            <input type="hidden" name="id" id="hos_id" value="" />
            <div class="fill-list">
                <span class="fill-tit">&nbsp;</span>
                <input class="fill-submit" type="button" id="subThisForm" value="提交">
            </div>
        </div>
    </div>
    <!-- content end -->
    <script>
        function upPop(id) {
            $('.upPop-box').upPop();
            $("#hos_id").val(id);
        }

        $(function() {
            $("#subThisForm").click(function () {
                  var hosId   =  $("#hos_id").val();
                  var depName = $("#classify_name").val();
                  $.ajax({
                      url:"{{route('Api.Admin.setHospitalDep')}}",
                      type: "post",
                      dataType:"json",
                      data:{hospital_id:hosId,dep_name:depName},
                      success:function (obj) {
                          if(obj.level == "success"){
                              window.location.reload();
                          }else {
                              alert(obj.message);
                          }
                      },
                      error:function (obj) {
                          console.log(obj);
                      }
                  });
            });

            $(document).on("click",".del_hospital",function(){
                var id = $(this).attr("data-id");
                noty({
                    text: '可能影响病人就诊，你确定要删除吗?',
                    buttons: [
                        {addClass: 'btn btn-primary', text: '确定', onClick: function($noty) {
                            $noty.close();
                            $.ajax({
                                url:'{{route('Api.Admin.delHospital')}}',
                                type:'post',
                                dataType:'json',
                                data:{id:id},
                                success:function(obj){
                                    if(obj.level == "success"){
                                        $("#hos_"+id).hide('slow');
                                    }else{
                                        noty({type:obj.level,text:obj.message,timeout:600});
                                    }
                                },
                                error:function(obj){
                                    console.log(obj);
                                }

                            });
                        }
                        },
                        {addClass: 'btn btn-danger', text: '取消', onClick: function($noty) {
                            $noty.close();
                        }
                        }
                    ]
                });

            });

            $(document).on("click",".del_dep",function(){
                var id = $(this).data("id");
                $.ajax({
                    url:'{{route('Api.Admin.delHospitalDep')}}',
                    type:'post',
                    dataType:'json',
                    data:{id:id},
                    success:function(obj){
                        if(obj.level == "success"){
                            $("#dep_"+id).hide('slow');
                        }else{
                            noty({type:obj.level,text:obj.message,timeout:600});
                        }
                    },
                    error:function(obj){
                        console.log(obj);
                    }

                });
            })

            $(document).on("click",".set_sort",function(){
                var id = $(this).data("id");
                $.ajax({
                    url:'{{route('Api.Admin.setHospitalSort')}}',
                    type:'post',
                    dataType:'json',
                    data:{id:id},
                    success:function(obj){
                        if(obj.level == "success"){
                            window.location.reload();
                        }else{
                            noty({type:obj.level,text:obj.message,timeout:600});
                        }
                    },
                    error:function(obj){
                        console.log(obj);
                    }

                });
            })
        });
    </script>
@endsection